package j2024.j202409;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;

import static java.lang.System.exit;

public class j0923 {
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.next();
        if(s.length()==0){
            System.out.println("No Answer");
            System.out.println(0);
            exit(0);
        }
        String str = s.toLowerCase();
        int[] hash = new int[26];
        int maxn = 0,minn = Integer.MAX_VALUE;
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            int tmp = (int)(ch-'a');
            hash[tmp]++;
            maxn = Math.max(maxn,hash[tmp]);
        }
        for (int i = 0; i < 26; i++) {
            if(hash[i]!=0){
                minn = Math.min(minn,hash[i]);
            }
        }
        int cou = maxn-minn;
        if(zhi(cou)){
            System.out.println("Lucky Word");
            System.out.println(cou);
        }else {
            System.out.println("No Answer");
            System.out.println(0);
        }
    }
    public static boolean zhi(int cou){
        if(cou==0){
            return false;
        }
        if(cou<=2){
            return true;
        }
        for (int i = 2; i < cou; i++) {
            if(cou%i == 0){
                return false;
            }
        }
        return true;
    }

    public static boolean hostschedule (int[][] schedule) {
        // write code here

        Arrays.sort(schedule,(a,b)->{
            return a[0]-b[0];
        });
        for (int i = 0; i < schedule.length; i++) {
            System.out.println(schedule[i][0]+" "+schedule[i][1]);
        }
        for (int i = 1; i < schedule.length; i++) {
            if(schedule[i][0]<schedule[i-1][1]){
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        int[][] a = new int[][]{{1,5},{10,15},{3,7}};
        hostschedule(a);
    }
    public static void main2(String[] args) {
           Scanner in = new Scanner(System.in);
           int n = in.nextInt();
           int[] num = new int[n];
           for (int i = 0; i < n; i++) {
               num[i] = in.nextInt();
           }
           Arrays.sort(num);
           System.out.println(sum(num,n));
    }
    public static boolean sum(int[] num,int n){
        int left = 0,right = n-1;
        int lSum = 0,rSum = 0;
        while(left<=right){
            if(lSum<rSum){
                lSum+=num[left++];
            }else if(lSum>rSum){
                rSum+=num[right--];
            }else {
                rSum+=num[right--];
                lSum+=num[left++];
            }
        }
        if(lSum==rSum){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 209. 长度最小的子数组
     * 给定一个含有 n 个正整数的数组和一个正整数 target 。
     *
     * 找出该数组中满足其总和大于等于 target 的长度最小的
     * 子数组
     *  [numsl, numsl+1, ..., numsr-1, numsr] ，并返回其长度。
     *  如果不存在符合条件的子数组，返回 0 。
     * @param target
     * @param nums
     * @return
     */
    public int minSubArrayLen(int target, int[] nums) {
        int left = 0,right = 0,sum = 0;
        int len = Integer.MAX_VALUE;
        while(left<=right && right<nums.length){
            if(sum<target){
                sum+=nums[right++];
            }
            while(left<=right && sum>=target){
                len = Math.min(right-left,len);
                sum-=nums[left--];
            }
        }
        if(len==Integer.MAX_VALUE){
            return 0;
        }
        return len;
    }

    /**
     *
     * 3. 无重复字符的最长子串
     * 给定一个字符串 s ，请你找出其中不含有重复字符的 最长
     * 子串
     *  的长度。
     *
     *
     * @param s
     * @return
     */
    public int lengthOfLongestSubstring(String s) {
        HashSet<Character> set = new HashSet<>();
        int left = 0,right = 0,len = 0;
        while(left<=right && right<s.length()){
            if(!set.contains(s.charAt(right))){
                set.add(s.charAt(right++));
            }else {
                //找出重复的那个
                while(s.charAt(left)!=s.charAt(right)){
                    set.remove(s.charAt(left++));
                }
                //找到重复的,此时s.charAt(left)==s.charAt(right)
                set.remove(s.charAt(left++));
                set.add(s.charAt(right++));
            }
            len = Math.max(len,right-left);
        }
        return len;
    }
    public int lengthOfLongestSubstring2(String s) {
        char[] str = s.toCharArray();
        int[] hash = new int[128];
        int left = 0,right = 0,len = 0;
        while(left<=right && right<s.length()){
            hash[str[right]]++;
            while(hash[str[right]]>1){
                hash[str[left++]]--;
            }
            len = Math.max(len,right-left+1);
            right++;
        }
        return len;
    }

}

